from django.db import models
from api.subject_management.models import Subject
from api.user_management.models import User
import django.utils.timezone as timezone


class Experiment(models.Model):
    """
    实验model，定义了实验相关属性
    """
    experiment_id = models.AutoField(max_length=20, primary_key=True, unique=True,
                                     verbose_name='experiment_id')  # 实验id
    experiment_name = models.TextField(null=False, blank=False,
                                       verbose_name='experiment_name')  # 实验名称
    subject_name = models.ForeignKey(Subject, on_delete=models.CASCADE, db_column='subject_name',
                                     verbose_name='subject_name')  # 课题名称
    experiment_num = models.TextField(max_length=50, null=False, blank=False,
                                      verbose_name='experiment_num')  # 实验编号
    experimenter = models.TextField(max_length=200, null=False, blank=False,
                                    verbose_name='experimenter')  # 实验人员
    # experiment_reviewer = models.ForeignKey(User, on_delete=models.CASCADE, default='')  # 审核人
    approval_status = models.TextField(max_length=10, null=True, blank=True, default='0',
                                       verbose_name='approval_status')  # 审核状态('0'表示已保存，'1'表示待审核，'2'表示审核通过)
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'experiment'  # 内嵌类指定实验表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('audit_experiment', u'审核实验'),
            ('view_experiment', u'查看实验'),
            ('add_experiment', u'添加实验'),
            ('delete_experiment', u'删除实验'),
            ('change_experiment', u'修改实验'),
        )


class ExperimentalProgram(models.Model):
    """
    实验方案model，定义了实验方案相关信息
    """
    experiment_program_id = models.AutoField(max_length=20, primary_key=True, unique=True,
                                             verbose_name='experiment_program_id')  # 实验方案id
    experiment_name = models.ForeignKey(Experiment, on_delete=models.CASCADE,
                                        db_column='experiment_id', verbose_name='experiment_name')  # 实验名称
    experiment_program_name = models.TextField(null=False, blank=False,
                                               verbose_name='experiment_program_name')  # 实验方案名称
    purpose = models.TextField(null=True, blank=True, verbose_name='purpose')  # 实验目的
    experimental_principle = models.TextField(null=True, blank=True,
                                              verbose_name='experimental_principle')  # 实验原理
    annex = models.FileField(upload_to="experimentProgram/annex/%Y/%m/%d/%H%M%S/", null=True, blank=True,
                             verbose_name='annex')  # 附件
    raw_material_name = models.TextField(null=True, blank=True,
                                         verbose_name='raw_material_name')  # 原料名称
    purity = models.TextField(null=True, blank=True,
                              verbose_name='purity')  # 纯度
    raw_material_source = models.TextField(null=True, blank=True,
                                           verbose_name='raw_material_source')  # 原料来源
    equipment_name = models.TextField(null=True, blank=True,
                                      verbose_name='equipment_name')  # 设备名称
    equipment_manufacturer = models.TextField(null=True, blank=True,
                                              verbose_name='equipment_manufacturer')  # 设备厂家
    device_model = models.TextField(null=True, blank=True,
                                    verbose_name='device_model')  # 设备型号
    quantity = models.TextField(null=True, blank=True,
                                verbose_name='quantity')  # 数量
    equipment_steps = models.TextField(null=True, blank=True,
                                       verbose_name='equipment_steps')  # 实验步骤
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'experiment_program'  # 内嵌类指定实验方案表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_experiment_program', u'查看实验方案'),
            ('add_experiment_program', u'添加实验方案'),
            ('delete_experiment_program', u'删除实验方案'),
            ('change_experiment_program', u'修改实验方案'),
        )


class ExperimentProcedure(models.Model):
    """
    实验过程model，定义了实验过程的相关属性
    """
    experiment_procedure_id = models.AutoField(max_length=20, primary_key=True, unique=True,
                                               verbose_name='experiment_procedure_id')  # 实验过程id
    experiment_name = models.ForeignKey(Experiment, on_delete=models.CASCADE,
                                        db_column='experiment_name', verbose_name='experiment_name')  # 实验名称
    experiment_program_name = models.ForeignKey(ExperimentalProgram, on_delete=models.CASCADE,
                                                db_column='experiment_program_name',
                                                verbose_name='experiment_program_name')  # 实验方案名称
    experiment_code = models.TextField(
        null=True, blank=True, verbose_name='experiment_code')  # 实验编码
    experimental_principle = models.TextField(null=True, blank=True,
                                              verbose_name='experimental_principle')  # 实验原理
    experiment_record = models.TextField(
        null=True, blank=True, verbose_name='experiment_record')  # 实验记录
    experiment_phenomena = models.TextField(
        null=True, blank=True, verbose_name='experiment_phenomena')  # 实验现象
    raw_material_name = models.TextField(null=True, blank=True,
                                         verbose_name='raw_material_name')  # 原料名称
    purity = models.TextField(null=True, blank=True,
                              verbose_name='purity')  # 纯度
    raw_material_source = models.TextField(null=True, blank=True,
                                           verbose_name='raw_material_source')  # 原料来源
    equipment_name = models.TextField(null=True, blank=True,
                                      verbose_name='equipment_name')  # 设备名称
    equipment_manufacturer = models.TextField(null=True, blank=True,
                                              verbose_name='equipment_manufacturer')  # 设备厂家
    device_model = models.TextField(null=True, blank=True,
                                    verbose_name='device_model')  # 设备型号
    quantity = models.TextField(null=True, blank=True,
                                verbose_name='quantity')  # 数量
    equipment_steps = models.TextField(null=True, blank=True,
                                       verbose_name='equipment_steps')  # 实验步骤
    sample_num = models.TextField(
        null=True, blank=True, verbose_name='sample_num')  # 样品编号
    experimental_expectation = models.TextField(null=True, blank=True,
                                                verbose_name='experimental_expectation')  # 实验预期
    # experiment_procedure_reviewer = models.ForeignKey(User, on_delete=models.CASCADE, default='')  # 审核人，可多选
    approval_status = models.TextField(max_length=10, null=True, blank=True, default='0',
                                       verbose_name='approval_status')  # 审核状态('0'表示已保存，'1'表示待审核，'2'表示审核通过)
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'experiment_procedure'  # 内嵌类指定实验过程表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('audit_experiment_procedure', u'审核实验过程'),
            ('view_experiment_procedure', u'查看实验过程'),
            ('add_experiment_procedure', u'添加实验过程'),
            ('delete_experiment_procedure', u'删除实验过程'),
            ('change_experiment_procedure', u'修改实验过程'),
        )


class Riddle(models.Model):
    """
    分析检测model，定义了分析检测的相关属性
    """
    riddle_id = models.AutoField(max_length=20, primary_key=True, unique=True,
                                 verbose_name='riddle_id')  # 分析检测id
    experiment_name = models.ForeignKey(Experiment, on_delete=models.CASCADE,
                                        db_column='experiment_name', verbose_name='experiment_name')  # 实验名称
    sample_num = models.TextField(
        null=False, blank=False, verbose_name='sample_num')  # 样品编号
    sample_name = models.TextField(
        null=False, blank=False, verbose_name='sample_name')  # 样品名称
    sample_status = models.TextField(
        null=True, blank=True, verbose_name='sample_status')  # 样品状态
    sample_quantity = models.TextField(
        null=True, blank=True, verbose_name='sample_quantity')  # 样品数量
    delivery_personnel = models.TextField(
        null=True, blank=True, verbose_name='delivery_personnel')  # 送样人员
    testing_facility = models.TextField(
        null=True, blank=True, verbose_name='testing_facility')  # 检测机构
    attorney_number = models.TextField(
        null=True, blank=True, verbose_name='attorney_number')  # 委托书编号
    test_num = models.TextField(
        null=True, blank=True, verbose_name='test_num')  # 检测编号
    delivery_date = models.DateTimeField(
        null=True, blank=True, verbose_name='delivery_date')  # 送样日期
    finish_date = models.DateTimeField(
        null=True, blank=True, verbose_name='finish_date')  # 完成日期
    riddle_report = models.FileField(upload_to='experimentManagement/riddleReport/%Y/%m/%d/%H%M%S/', null=True, blank=True,
                                     verbose_name='riddle_report')  # 分析检测报告
    main_testing_equipment = models.TextField(null=True, blank=True,
                                              verbose_name='main_testing_equipment')  # 主检测设备
    experiment_amount = models.TextField(
        null=True, blank=True, verbose_name='experiment_amount')  # 实验金额
    annex = models.FileField(upload_to='experimentManagement/annex/%Y/%m/%d/%H%M%S/', null=True, blank=True,
                             verbose_name='annex')  # 附件
    add_date = models.DateTimeField(default=timezone.now)  # 保存时间
    mod_date = models.DateTimeField(auto_now=True)  # 最后修改时间

    class Meta:
        db_table = 'riddle'  # 内嵌类指定分析检测表表名
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_riddle', u'查看分析检测'),
            ('add_riddle', u'添加分析检测'),
            ('delete_riddle', u'删除分析检测'),
            ('change_riddle', u'修改分析检测'),
        )
